package com.dongjiantong.common.redis;

import com.dongjiantong.common.utils.PropertiesLoaderUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author cao
 * @create 2018-01-02 下午5:53
 **/

public class JedisUtil {

    //// 主机号,默认是本地
    private static String host = "127.0.0.1";

    // 端口号
    private static int port = 6379;

    // redis密码 认证
    private static String password = "";

    // 是否使用redisPool
    private static boolean isPool = true;

    // 设置默认超时时间10s
    private static int timeout = 100000;

    // redis池
    private static JedisPool jedisPool;


    static {
        initRedis();
    }

    /**
     * 初始化redis操作
     */
    private static void initRedis() {

        try {
            PropertiesLoaderUtils properties = new PropertiesLoaderUtils("redis.properties");

            // 判断使用环境
            // String proisPool = properties.getProperty("redis.isPool");
            String prodEvn=properties.getProperty("redis.prod");

            if("true".equals(prodEvn))
            {
                host = properties.getProperty("redis.host");
                port = Integer.parseInt(properties.getProperty("redis.port"));
                password = properties.getProperty("redis.password");
            }
            else
            {
                host = properties.getProperty("redis.host.dev");
                port = Integer.parseInt(properties.getProperty("redis.port.dev"));
                password = properties.getProperty("redis.password.dev");
            }

//            String proHost = properties.getProperty("redis.host");
//            String proPort = properties.getProperty("redis.port");
//            String proPassword = properties.getProperty("redis.password");
//
//            // 设置变量
//            if (proHost != null && proPort != null && proPassword != null) {
//                host = proHost;
//                port = Integer.parseInt(proPort);
//                password = proPassword;
//            }

            System.out.println("RedisPool开始创建");

            JedisPoolConfig poolConfig = new JedisPoolConfig();

            ////最大连接数
            poolConfig.setMaxTotal(5000);
            //最大空闲连接数
            poolConfig.setMaxIdle(500);
            //设置最小空闲数
            poolConfig.setMinIdle(8);
            poolConfig.setMaxWaitMillis(10000);
            poolConfig.setTestOnBorrow(true);
            poolConfig.setTestOnReturn(true);
            //Idle时进行连接扫描
            poolConfig.setTestWhileIdle(true);
            //表示idle object evitor两次扫描之间要sleep的毫秒数
            poolConfig.setTimeBetweenEvictionRunsMillis(30000);
            //表示idle object evitor每次扫描的最多的对象数
            poolConfig.setNumTestsPerEvictionRun(10);
            //表示一个对象至少停留在idle状态的最短时间，然后才能被idle object evitor扫描并驱逐；这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
            poolConfig.setMinEvictableIdleTimeMillis(60000);



            if(password.equals(""))
            {
                jedisPool = new JedisPool(poolConfig, host, port, timeout);
            }
            else {
                jedisPool = new JedisPool(poolConfig, host, port, timeout, password);
            }
            System.out.println("RedisPool创建完成");

        } catch (Exception e) {

            System.out.println("加载的redis.properties文件失败");
            e.printStackTrace();
        }
    }



    /**
     * 释放jedis和jedisPool资源
     * @param jedis
     */
    public static void releaseResource(final Jedis jedis){
//        if (jedis == null){
//            jedis.close();
//        }
//        if (jedisPool == null){
//            jedisPool.close();
//
//        }
        if (jedis != null){
            jedis.close();
        }

    }



    /**
     * 获取jedis实例
     * @return
     */
    public synchronized static Jedis getJedis(){
        //连接池中获取资源
        try{
            if (jedisPool != null){
                Jedis jedis = jedisPool.getResource();
                return jedis;
            }else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
